package edu.ucsd.cse110;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import edu.ucsd.cse110.model.Log;

/**
 * Displays a list of all log entries of a specific WOD.
 */
public class StatsWodDetailActivity extends ListActivity {
	private static int id;
	private static String wod = "";

	/**
	 * TODO: Need documentation.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		wod = getIntent().getExtras().getString("wodString");
		id = getIntent().getExtras().getInt("id");
		setTitle("" + wod + " Details");
		Format formatter = new SimpleDateFormat("dd-MMM-yyyy");
		ArrayList<String> wodDetailList = new ArrayList<String>();

		LocalDatabaseManager database = new LocalDatabaseManager(this);
		List<Log> wodList = database.getAllLog();
		database.close();

		String resultString = "";

		for (int i = 0; i < wodList.size(); i++) {
			if (wodList.get(i).getWodId() == id) {
				resultString = "Date: "
						+ formatter.format(wodList.get(i).getDate().getTime());
				if ((wodList.get(i).getMinute() > 0)
						|| (wodList.get(i).getSecond() > 0)) {
					resultString = resultString + "\nTime Completed: "
							+ wodList.get(i).getMinute() + " Minute(s) "
							+ wodList.get(i).getSecond() + " Second(s)";
				}

				if (wodList.get(i).getRep() > 0) {
					resultString = resultString + "\nReps: "
							+ wodList.get(i).getRep();
				}

				if (wodList.get(i).getRound() > 0) {
					resultString = resultString + "\nRounds: "
							+ wodList.get(i).getRound();
				}
				wodDetailList.add(resultString);
				/*
				 * "Date: " +
				 * formatter.format(wodList.get(i).getDate().getTime()) +
				 * "\nTime Completed: " + wodList.get(i).getMinute() +
				 * " Minute(s) " + wodList.get(i).getSecond() + " Second(s)");
				 */
			}
		}

		setListAdapter(new ArrayAdapter<String>(this, R.layout.wod_details,
				wodDetailList));

		ListView lv = getListView();
		lv.setTextFilterEnabled(true);
	}
}